package com.jy.api.wechat.handler;


import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.jy.api.dao.CouponInstanceDao;
import com.jy.api.dao.MemberDao;
import com.jy.api.domain.CouponInstance;
import com.jy.api.domain.Member;
import com.jy.api.service.WechatMemberService;
import com.jy.api.wechat.builder.TextBuilder;
import com.jy.api.wechat.service.BaseWxService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@Slf4j
public abstract class ScanHandler extends AbstractHandler {


    @Autowired
    WechatMemberService wechatMemberService;

    @Autowired
    private CouponInstanceDao instanceDao;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) throws WxErrorException {

        this.logger.info("已关注用户扫码 OPENID: " + wxMessage.getFromUser());

        BaseWxService weixinService = (BaseWxService) wxMpService;
        WxMpXmlOutMessage responseResult = null;
        String msg = "关注成功！祝你生活愉快！";
        try {
            if (wxMessage.getEventKey().indexOf("couponInstanceId") != -1 ||
                    wxMessage.getEventKey().indexOf("shareId") != -1) {
                String eventKey = wxMessage.getEventKey();
                String info = eventKey.split("=")[1];
                JSONObject jsonObject = JSONObject.fromObject(info);
                String couponInstanceId = jsonObject.getString("shareId");
                CouponInstance coupon = instanceDao.findOne(couponInstanceId);
                CouponInstance couponInstance = instanceDao.findByOpenIdAndCouponId(wxMessage.getFromUser(), coupon.getCouponId());
                if (couponInstance != null) {
                    msg = "您已拥有该优惠券，请勿重新领取";
                } else {
                    msg = "领取成功";
                }
            }
            responseResult = wechatMemberService.handleSpecial(wxMessage, weixinService);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }

        if (responseResult != null) {
            return responseResult;
        }
        System.out.println(JSON.toJSONString(wxMessage));
        try {

            if (wxMessage.getEventKey().indexOf("referralId") != -1) {
                msg = "感谢关注";
            }
            if (wxMessage.getEventKey().indexOf("orderId") != -1) {
                msg = "登陆成功";
            }

            return new TextBuilder().build(msg, wxMessage, weixinService);
        } catch (Exception e) {
            this.logger.error("登陆失败：{}", e.getMessage(), e);
            return new TextBuilder().build("登陆失败！", wxMessage, weixinService);
        }

    }

}
